<?php

declare(strict_types=1);

namespace App\BO;

use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Request;

class UserLocationBO
{
    public const USER_DEFAULT_CITY_ID = 1;
    public const USER_LOCATION_CITY_REDIS_KEY = 'hash.userlocationcity';

    public static function getUserCityId($userId): int
    {
        $cityId = (int) Redis::connection()->hGet(self::USER_LOCATION_CITY_REDIS_KEY, $userId);
        if (!$cityId) {
            $cityId = self::getDefaultCityId();
            self::setUserCityId($userId, $cityId);
        }
        return $cityId;
    }

    public static function setUserCityId($userId, $cityID): void
    {
        Redis::connection()->hSet(self::USER_LOCATION_CITY_REDIS_KEY, $userId, $cityID);
    }

    public static function getDefaultCityId()
    {
        return self::USER_DEFAULT_CITY_ID;
    }

    public static function getRequestCityId()
    {
        $reqCityId = (int) Request::get('city_id');
        return $reqCityId;
    }
}
